package com.mad.bakergps;

import java.util.ArrayList;

import android.util.Log;

public class pathFinding {
	private 
		class Path{
			public
				ArrayList<Node> nodes=new ArrayList<Node>();
				ArrayList<Line> lines=new ArrayList<Line>();
				Path(){}
				Path(Path argpath)
				{
					nodes=new ArrayList<Node>(argpath.nodes);
					lines=new ArrayList<Line>(argpath.lines);
					
				}
		}
		ArrayList<Path> pathList= new ArrayList<Path>();
		Path shortestPath;
		Node closestNode;
		Node startNode;
		Node endNode;
		Line currentLine;
		Room destinationRoom;
		Building destinationBuilding;
		void findPath(Node argNode, Path workingPath) {
			
			if (argNode!=endNode) //only recurse if this is not the end node
				for(Line i:argNode.outGoingLines)
				{
					if (i.leadsTo(endNode)){ //if this is never going to get to the end node, stop now
						Path nextWorkingPath= new Path(workingPath); //make a new path from workingPath as to not copy memory location
						nextWorkingPath.nodes.add(argNode); //add the next node along the path
						nextWorkingPath.lines.add(i); // add the next line along the path
						findPath(i.endNode, nextWorkingPath); //recurse to find continue the path
					}
				}
			else {
				pathList.add(workingPath); //if we reached the end point, add the path to the list of paths.
			}
			
		}
		
		void findShortestPath() {
			int lowestDistance=100000; //give an arbitrary large number
			int lowestIndex=0; //starting index
			for(int i=0;i<=pathList.size();i++){ //for every path in path list
				int distance=0;
				for(Line n:pathList.get(i).lines){ // for every line in path "i"
					distance+=n.distanceInFeet;	//add each line's distance to "distance"
				}
				
				if(distance<lowestDistance) //if its the lowest distance record that distance and its index
				{
					lowestDistance=distance;
					lowestIndex=i;
				}
			}
			
			shortestPath=pathList.get(lowestIndex);
			
		}
	public 
		pathFinding(){}
		pathFinding(Node argStartNode, Node argEndNode,Room argDestinationRoom,Building argDestinationBuilding)
		{
			startNode = argStartNode;
			endNode= argEndNode;
			destinationRoom= argDestinationRoom;
			destinationBuilding=argDestinationBuilding;
			
		}
		
		void makePath() throws Exception{
			if(startNode==null){
				Log.v("PathFinding", "UnIntilized startNode");
				throw new Exception("UnInitilized startNode in Pathfinding");
			}
			if (endNode==null)
			{
				Log.v("PathFinding", "UnInitilized endNode");
				throw new Exception("UnInitilized endNode in Pathfinding");
			}
				findPath(startNode,new Path());
				findShortestPath();
			
		}
		
		

		void findClosestNode() {
			// TODO figure out how to find and returnCloesestNode, use gps or fingerpressing
			
		}
		ArrayList pathPictures() throws Exception{
			if(shortestPath==null)
				makePath();
			//TODO: Return Pictures in pathPictures()
			
			return null;
		}

}
